#!/bin/bash
# 版本: 1.0
# 作者: huangsheng
# 兼容系统版本:CentOS 7.9 Rocky Linux 8.7 Rocky Linux 8.8
# 验证:未验证

os_version=$(cat /etc/os-release | grep '^ID=' | cut -d'=' -f2)
os_release=$(cat /etc/os-release | grep 'VERSION_ID=' | cut -d'=' -f2 | tr -d '"')

echo "检测到的操作系统: $os_version $os_release"

if [[ "$os_version" == "centos" || "$os_version" == "rocky" ]]; then
    if [[ "$os_version" == "centos" && "$os_release" == "7.9" ]]; then
        # CentOS 7.9
        yum install -y https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
    elif [[ "$os_version" == "rocky" && ( "$os_release" == "8.7" || "$os_release" == "8.8" ) ]]; then
        # Rocky Linux 8.7 / 8.8
        yum install -y https://www.elrepo.org/elrepo-release-8.el8.elrepo.noarch.rpm
    else
        echo "不支持的操作系统版本: $os_version $os_release"
        exit 1
    fi
    
    yum --enablerepo=elrepo-kernel install -y kernel-ml

    if [[ "$os_version" == "centos" && "$os_release" == "7.9" ]]; then
        grub2-set-default 0
        grub2-mkconfig -o /boot/grub2/grub.cfg
    elif [[ "$os_version" == "rocky" && ( "$os_release" == "8.7" || "$os_release" == "8.8" ) ]]; then
        grub2-set-default 0
        grub2-mkconfig -o /boot/grub2/grub.cfg
    fi
    echo "内核升级完成。请重启系统以应用新内核。"
else
    echo "该脚本仅支持 CentOS 7.9 和 Rocky Linux 8.7/8.8。"
    exit 1
fi
